package com.mfs.common.utills;

import java.io.*;

/**
 * 对象工具类
 * @author mfs
 * @date 2022/05/18
 * */
public class ObjectUtil {

    /**
     * 序列化对象
     * */
    public static byte[] serialize(Object o) {
        byte[] bytes = new byte[0];

        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
            objectOutputStream.writeObject(o);
            bytes = byteArrayOutputStream.toByteArray();
        } catch (IOException e) {

        }
        return bytes;
    }
    /**
     * 反序列化对象
     * */
    public static <T>T deserialize(byte[] bytes, Class<T> tClass) {
        T t = null;
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
             ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
            t = (T) objectInputStream.readObject();
        } catch (ClassNotFoundException | IOException e) {

        }
        return t;
    }
}
